package faf.business.tests;

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;

import javax.persistence.EntityManager;

import junit.framework.Assert;

import org.junit.BeforeClass;
import org.junit.Test;

import faf.business.UserManager;
import faf.dao.JpaUtils;
import faf.entities.Account;

public class UserManagerTests {
	static JpaUtils jpautils;
	static UserManager manager;
	
	@BeforeClass
	public static void beforeClass() {
		HashMap<String, String> config = new HashMap<String, String>();
		config.put("hibernate.hbm2ddl.auto", "create-drop");
		jpautils = new JpaUtils(config);
		manager = new UserManager();
		manager.setJpautils(jpautils);
	}

	/**
	 * Test method for {@link faf.business.UserManager#createAccount(faf.entities.Account)}.
	 */
	@Test
	public void createAccountNewTests() throws NoSuchAlgorithmException {
		EntityManager em = jpautils.createEntityManger();
		
		Account account = new Account();
		account.setFirstname("test");
		account.setLastname("test");
		account.setAdministrator(false);
		account.setMail("test@example.net");
		account.setRedactor(false);
		account.setPassword("password");
		
		Assert.assertNotNull(manager.createAccount(account));
	}

	/**
	 * Test method for {@link faf.business.UserManager#createAccount(faf.entities.Account)}.
	 */
	@Test
	public void createAccountDuplicateTests() throws NoSuchAlgorithmException {
		EntityManager em = jpautils.createEntityManger();
		
		Account account = new Account();
		account.setFirstname("test");
		account.setLastname("test");
		account.setAdministrator(false);
		account.setMail("test@example.net");
		account.setRedactor(false);
		
		account = new Account();
		account.setFirstname("test");
		account.setLastname("test");
		account.setAdministrator(false);
		account.setMail("test@example.net");
		account.setRedactor(false);
		
      
		em.getTransaction().begin();
		em.persist(account);
		em.getTransaction().commit();
		
		Assert.assertNull(manager.createAccount(account));
	}
}
